using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using NUnit.Framework;
using Hashes;
using Hashes.StringHashes;
using ECollections;

namespace UnitTests
{
    [TestFixture]
    public class ECollectionsTest
    {
        [Test]
        public void TestDictionary()
        {
            try
            {
                DJB2 hashProviderA = new DJB2();
                SDBM hashProviderB = new SDBM();
                ECollections.Dictionary<string, int> dictionary = new ECollections.Dictionary<string, int>(1024, hashProviderA, hashProviderB);

                StreamReader reader = File.OpenText("WordList.txt");
                int addedCount = 0;
                string readLine = string.Empty;

                while ((readLine = reader.ReadLine()) != null)
                {
                    dictionary.Add(readLine, 0);
                    addedCount++;
                }

                reader.Close();
                reader = File.OpenText("WordList.txt");
                while ((readLine = reader.ReadLine()) != null)
                {
                    if (dictionary.ContainsKey(readLine))
                        addedCount--;
                    else
                        continue;
                }

                if (addedCount > 0)
                {
                    Console.WriteLine("Added keys could not be retrieved, difference: " + addedCount.ToString());
                    Assert.Fail("Test Failed, added keys could not be retrieved");
                }
            }
            catch(Exception ex)
            {                
                Assert.Fail("Unhandled exception: \r\n" + ex.ToString());
            }
        }

        [Test]
        public void TestStringDictionary()
        {
            try
            {
                DJB2 hashProviderA = new DJB2();
                SDBM hashProviderB = new SDBM();
                ECollections.StringDictionary<int> dictionary = new ECollections.StringDictionary<int>(1024);

                StreamReader reader = File.OpenText("WordList.txt");
                int addedCount = 0;
                string readLine = string.Empty;

                while ((readLine = reader.ReadLine()) != null)
                {
                    dictionary.Add(readLine, 0);
                    addedCount++;
                }

                reader.Close();
                reader = File.OpenText("WordList.txt");
                while ((readLine = reader.ReadLine()) != null)
                {
                    if (dictionary.ContainsKey(readLine))
                        addedCount--;
                    else
                        continue;
                }

                if (addedCount > 0)
                {
                    Console.WriteLine("Added keys could not be retrieved, difference: " + addedCount.ToString());
                    Assert.Fail("Test Failed, added keys could not be retrieved");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail("Unhandled exception: \r\n" + ex.ToString());
            }
        }

        [Test]
        public void TestStringValueBag()
        {
            try
            {
                DJB2 hashProviderA = new DJB2();
                SDBM hashProviderB = new SDBM();
                ECollections.StringValueBag<int> dictionary = new ECollections.StringValueBag<int>(1024);

                StreamReader reader = File.OpenText("WordList.txt");
                int addedCount = 0;
                string readLine = string.Empty;

                while ((readLine = reader.ReadLine()) != null)
                {
                    dictionary.Add(readLine, 0);
                    addedCount++;
                }

                reader.Close();
                reader = File.OpenText("WordList.txt");
                while ((readLine = reader.ReadLine()) != null)
                {
                    if (dictionary.ContainsKey(readLine))
                        addedCount--;
                    else
                        continue;
                }

                if (addedCount > 0)
                {
                    Console.WriteLine("Added keys could not be retrieved, difference: " + addedCount.ToString());
                    Assert.Fail("Test Failed, added keys could not be retrieved");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail("Unhandled exception: \r\n" + ex.ToString());
            }
        }
    }
}
